package com.melody.android.ui.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.melody.android.R
import com.melody.android.logic.model.RecommendSongList
import com.melody.android.logic.util.showToast
import com.melody.android.ui.activity.SongListActivity

class RecommendSongListAdapter(private val recommendSongLists: List<RecommendSongList>) : RecyclerView.Adapter<RecommendSongListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val recommendSongListCover: ImageView = view.findViewById(R.id.recommend_song_list_cover)
        val recommendSongListDescribe: TextView = view.findViewById(R.id.recommend_song_list_describe)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_recommend_song_list, parent, false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val recommendSongList = recommendSongLists[position]

            recommendSongList.describe.showToast()
            // 跳转到SongListActivity
            val intent = Intent(parent.context, SongListActivity::class.java)
            intent.putExtra("songListDescription", recommendSongList.describe)
            parent.context.startActivity(intent)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val recommendSongList = recommendSongLists[position]
        holder.recommendSongListCover.setImageResource(recommendSongList.cover)     // 推荐歌单的封面
        holder.recommendSongListDescribe.text = recommendSongList.describe          // 推荐歌单的描述
    }

    override fun getItemCount() = recommendSongLists.size

}